<?php 
namespace App\Http\Controllers\MobileApi;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class DictController extends Controller
{
	use GlobalData;

	const ROUTE_PREFIX = '/api/*';

	protected $request;

	protected $curTime;

	protected $debug;


	/**
	 * 创建新的`DictController`控制器实例
	 * @param \Illuminate\Http\Request $request
	 */
	public function __construct(Request $request)
	{
		$this->request = $request;
		$this->error = [];
		$this->curTime = time();

		if(config('app.debug')) {
			$this->debug = true;
			error_reporting(E_ALL | E_STRICT);
		} else {
			$this->debug = false;
		}
	}

    #查询接口
    public function Lookup()
    {

        $p = $_REQUEST;
        $key =isset($p['key']) ?  addslashes(trim($p['key'])) : '';
        if(empty($key))
        {
            $data = array(
                'code' => 5,
                'msg' => '亲，术语不能为空',
            );
            exit(json_encode($data));
        }
		$r = $this->get_from_iol8($key);
        $data = array(
            'code' => 200,
            'msg' => '查询术语结果成功返回',
			'result' => $r,
        );
        exit(json_encode($data));
    }

	//https请求（支持GET和POST）此方法经过微信测试也完全可以
	public function https_request($url, $data = null)
	{
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		//curl_setopt($curl, CURLOPT_SSLVERSION,CURL_SSLVERSION_TLSv1);
		curl_setopt($curl, CURLOPT_SSLVERSION, 1);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
		if (!empty($data)){
			curl_setopt($curl, CURLOPT_POST, 1);
			curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		}
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($curl);
		curl_close($curl);
		return $output;
	}
	
	public function get_from_iol8($key)
	{
		//$key='金刚经';
		//$key='居士';
		// $key='禅语';
		//echo "key=$key<br/>";
		$url="http://longquan.iol8.com/api.php?key={$key}";
		$data=$this->https_request($url, $data = null);#get请求 目前$key 参数只有“居士” 有参数
		$data=json_decode($data,true);
	//    echo '<pre>';
	//    print_r($data);
		// echo json_encode($data);
		return $data;
	}
	
    #测试messagecutstr
    public function Cut()
    {

        $str = "[b]a\r\nb[/b]";
        file_put_contents("\lishu.txt",messagecutstr($str),8) ;
    }

	
} //!- DictController
?>
